package com.fly.container.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

/**
 * @author Mr.Fu
 * @date 2023/4/16 下午11:14
 */
@Service
public class RetryService implements InitializingBean, BeanFactoryAware{//, BeanPostProcessor {//, BeanFactoryPostProcessor {

    @Autowired
    private DepartmentService departmentService;

    @Retryable(value = Exception.class, maxAttempts = 5, backoff = @Backoff(delay = 2000, multiplier = 1.5))
    public String retry(String name) {
        return check(name);
    }

    public String check(String name) {
        departmentService.print("your param is " + name);
        if ("Tom".equals(name)) {
            throw new RuntimeException("Tom is not support request this method.");
        } else {
            return "SUCCESS";
        }
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanFactoryAware.setBeanFactory 方法被执行 => beanFactory" + beanFactory.toString());
        System.out.println("BeanFactoryAware.setBeanFactory departmentService " + departmentService);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean.afterPropertiesSet 方法被执行");
        System.out.println("InitializingBean.afterPropertiesSet departmentService " + departmentService);
    }

//    @Override
//    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
//        System.out.println("BeanFactoryPostProcess.postProcessBeanFactory 方法被执行 beanFactory => " + configurableListableBeanFactory.toString());
//        System.out.println("BeanFactoryPostProcess.postProcessBeanFactory departmentService " + departmentService);
//    }

//    @Override
//    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//        System.out.println("BeanPostProcess.postProcessBeforeInitialization 方法被执行 beanName => " + beanName);
//        System.out.println("BeanPostProcess.postProcessBeforeInitialization departmentService " + departmentService);
//        return bean;
//    }
//
//    @Override
//    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//        System.out.println("BeanPostProcess.postProcessAfterInitialization 方法被执行 beanName => " + beanName);
//        System.out.println("BeanPostProcess.postProcessAfterInitialization departmentService " + departmentService);
//        return bean;
//    }
}
